<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of email_system
 *
 * @author hassanali
 */
class email_system {

    public function GetEmailtemplate($file = '', $includefile = 0) {
        $CI = & get_instance();
        if (!empty($file)) {
            $CI->load->helper('file');
            if ($includefile === 1) {
                $header = APPPATH . 'views/' . ADMIN_FOLDER . '/helper/email/header.html';
                $footer = APPPATH . 'views/' . ADMIN_FOLDER . '/helper/email/footer.html';
                $path = APPPATH . 'views/' . ADMIN_FOLDER . '/helper/email/' . $file . '.html';

                $template = read_file($header);
                $template.=read_file($path);
                $template.=read_file($footer);
                return $template;
            } else {
                $path = APPPATH . 'views/' . ADMIN_FOLDER . '/helper/email/' . $file . '.html';
                return read_file($path);
            }
        } else {
            return '';
        }
    }

    /** Function To send email using smtp system default by gmail
     *  You can change email setting from APPPATH/config/email.php
     *  You can change smtp value from admin setting db table setting
     *  
     *  return true if send Success  | return false if send error
     * @param type $email
     * @param type $subject
     * @param type $template file format html from path : APPATH/views/adminfolder/helper/email/filename.html
     * @param type $replace  Code in hml template You wante to replace it
     * @param type $attach if used 
     * @return boolean
     */
    public function SendEmail($email, $subject, $template = false, $replace = false, $attach = false, $message = false) {
        global $CI;
        //================ format message =================
        if ($template) {
            $message = '';
            $template = $this->GetEmailtemplate($template);
            $var = $this->get_template_var($template);
            $message = $this->format_message($template, $var, $replace);
        }
        //================ format message ================
        if ($_SERVER['HTTP_HOST'] != 'localhost') {
            $CI->load->library('email');
            $CI->email->clear(TRUE);
            $CI->email->to($email);
            $CI->email->from(get_option('site_email'));
            $CI->email->subject($subject);
            $CI->email->message($message);
            if (!empty($attach)) {
                $this->email->attach($attach);
            }

            $send = $CI->email->send();
            if ($send) {
                $CI->email->clear();
                return true;
            } else {
                return $CI->email->print_debugger();
            }
        }
    }

    private function get_template_var($template) {
        // get all {data} from rempalte
        $result = array();
        $pattern = "/{(.*?)}/";
        preg_match_all($pattern, strip_tags($template), $matches);
        if (count($matches[1]) > 0) {
            foreach ($matches[1] as $value) {
                $result[$value] = ' ';
            }
        }


        return $result;
    }

    private function format_message($template = '', $search_replace = array(), $replace = array()) {
        $message = '';
        $r_rpalce = array();


        $search_replace = array_merge($search_replace, $this->default_var());

        $search_replace = array_merge($search_replace, $replace);

        if (count($search_replace) > 0) {
            foreach ($search_replace as $key => $value) {
                $r_rpalce['{' . $key . '}'] = $value;
            }

            $key = array_keys($r_rpalce);
            $val = array_values($r_rpalce);
            $message.=str_replace($key, $val, $template);
        }
        return $message;
    }

    private function default_var() {
        $default = array();
        $default['SiteEmail'] = get_option('site_email');
        $default['SiteUrl'] = base_url();
        $default['SiteTitle'] = get_option('site_title');
        return $default;
    }

}

?>
